package com.zjcode.cas.service.impl;

import com.zjcode.cas.mapper.TkmSysExMapper;
import com.zjcode.cas.mapper.TkmSysUserMapper;
import com.zjcode.cas.model.TkmSysEx;
import com.zjcode.cas.model.TkmSysUser;
import com.zjcode.cas.service.TkmSysExService;
import com.zjcode.cas.utils.SecurityUtil;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @ClassName:TkmSysExServiceImpl
 * @功能：
 * @author：wlei
 * @日期：2022/10/20-21:09
 **/
@Service
@Slf4j
public class TkmSysExServiceImpl implements TkmSysExService {

    @Autowired
    private TkmSysExMapper sysExMapper;
    @Autowired
    private TkmSysUserMapper sysUserMapper;

    @Override
    public int insert(TkmSysEx record) {
        String sourceId = record.getSourceId();
        String billNo = record.getBillNo();
        // 如果数据已经存在，则跳过执行
        if (findBySourceId(sourceId) != null) {
            log.info("扩展信息表-数据已存在，跳过插入步骤");
            return 0;
        }
        // 执行加密操作
        try {
            sourceId = SecurityUtil.decode(sourceId);
            billNo = SecurityUtil.decode(billNo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        record.setSourceId(sourceId);
        record.setBillNo(billNo);
        int insert = sysExMapper.insert(record);
        log.info("扩展信息表-数据插入结果:{}", insert > 0 ? "成功" : "失败");
        return insert;
    }

    @Override
    public TkmSysEx findBySourceId(String sourceId) {
        try {
            sourceId = SecurityUtil.decode(sourceId);
            List<TkmSysEx> tkmSysExes = sysExMapper.selectBySourceId(sourceId);
            if (!CollectionUtils.isEmpty(tkmSysExes)) {
                return tkmSysExes.get(0);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public String findBillNoBySourceId(String sourceId) {
        String result = "";
        try {
            sourceId = SecurityUtil.decode(sourceId);
            List<TkmSysEx> tkmSysExes = sysExMapper.selectBySourceId(sourceId);
            if (!CollectionUtils.isEmpty(tkmSysExes)) {
                TkmSysEx tkmSysEx = tkmSysExes.get(0);
                String billNo = tkmSysEx.getBillNo();
                String decrypt = SecurityUtil.decrypt(billNo);
                result = decrypt;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public int deleteBySourceId(String sourceId) {
        int count = 0;
        try {
            sourceId = SecurityUtil.decode(sourceId);
            count = sysExMapper.deleteBySourceId(sourceId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public TkmSysUser findUserByPhone(String phone) {
        return sysUserMapper.findOneByUsernameorderByCreateTimeDesc(phone);
    }
}
